/**
 *
 * 2007-11-5 下午09:28:29
 * @author xuhuan
 */

package edu.ccut.saturn.studio.logic.model.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDataNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;

public class DataLoader extends NodeLoader {

	private static final String DATA_LOADER_NULL = "";

	@Override
	public List<Shape> loadNodes(Element root) {
		NodeList dataNodes = root
				.getElementsByTagName(SaturnDataNode.KEY_NODE_DATA);
		List<Shape> shapes = new ArrayList<Shape>(dataNodes.getLength());
		for (int i = 0; i < dataNodes.getLength(); i++) {
			String classtype = DATA_LOADER_NULL, namespace = DATA_LOADER_NULL, saturndatatype = DATA_LOADER_NULL;
			SaturnDataNode saturnDataNode = new SaturnDataNode();

			Element dataNode = (Element) dataNodes.item(i);

			String shapeId = super.getNodeAttributeValue(dataNode,
					Shape.KEY_ATTRIBUTE_SHAPE_ID);
			String nextId = super.getNodeAttributeValue(dataNode,
					Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID);
			String scope = super.getNodeElementValue(dataNode,
					SaturnDataNode.SHOW_DATA_NODE_SCOPE);
			String prefix = super.getNodeElementValue(dataNode,
					SaturnDataNode.SHOW_DATA_NODE_PREFIX);
			String datakey = super.getNodeElementValue(dataNode,
					SaturnDataNode.SHOW_DATA_NODE_DATA_KEY);
			String description = super.getNodeElementValue(dataNode,
					SaturnDataNode.SHOW_DATA_NODE_DESCRITPION);

			String name = super.getNodeElementValue(dataNode,
					SaturnDataNode.SHOW_DATA_NODE_NAME);
			List<SaturnParameter> parameters = super.getParameters(dataNode);
			NodeList childNodes = dataNode
					.getElementsByTagName(SaturnDataNode.SHOW_DATA_NODE_DATATYPE);

			for (int j = 0; j < childNodes.getLength(); j++) {
				Node dataTypeNode = childNodes.item(j);
				classtype = super.getNodeAttributeValue(dataTypeNode,
						SaturnDataNode.SHOW_DATA_NODE_DATA_CLASSTYPE);
				namespace = super.getNodeAttributeValue(dataTypeNode,
						SaturnDataNode.SHOW_DATA_NODE_DATA_NAMESPACE);
				saturndatatype = super.getNodeAttributeValue(dataTypeNode,
						SaturnDataNode.SHOW_DATA_NODE_DATA_SATURNDATATYPE);
			}

			Point location = super.getLocation(dataNode);
			Dimension size = new Dimension(50, 70);
			saturnDataNode.setNode(dataNode);
			saturnDataNode.setName(name);
			saturnDataNode.setDescription(description);
			saturnDataNode.setId(shapeId);
			saturnDataNode.setNextId(nextId);
			saturnDataNode.setClasstype(classtype);
			saturnDataNode.setDatakey(datakey);
			saturnDataNode.setNamespace(namespace);
			saturnDataNode.setLocation(location);
			saturnDataNode.setSize(size);
			saturnDataNode.setSaturndatatype(saturndatatype);
			saturnDataNode.setPrefix(prefix);
			saturnDataNode.setScope(scope);
			saturnDataNode.setParameters(parameters);
			saturnDataNode.setXmlKey(super.getNodeElementValue(dataNode,SaturnDataNode.SHOW_DATA_NODE_DATA_XMLKEY ));
			saturnDataNode.setElementKey(super.getNodeElementValue(dataNode, SaturnDataNode.SHOW_DATA_NODE_DATA_ELEMENT ));
			shapes.add(saturnDataNode);

		}
		return shapes;

	}

}
